﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Simple.Framework.Configuration;
using Simple.Framework.Ioc;
using Simple.Framework.Logger;
using Simple.Framework.Schedule;
using Simple.PrettyNet.Handle;

/*********************************************************
 * 命名空间 Simple.BasicNet.Core.Net
 * 接口名称 Host
 * 开发人员：11920
 * 创建时间：2023/4/6 15:33:15
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.PrettyNet.Net
{
    public class Host: IHost
	{
		IContainer container;
        IConfiguration serviceConfigution;
		ILogger logger;
		IPretty prettyCore { get; set; }
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
        public static IHost BuilderHost()
		{
			return new Host();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public IHost Start()
		{
			var scheduleManager = container.GetService<IScheduleManager>();
			scheduleManager.Start();
			prettyCore = container.GetService<IPretty>();
			prettyCore.Start();
			return this;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="ConfigurationPath"></param>
		/// <returns></returns>
		public IHost SetConfiguration<TConfiguration>(string ConfigurationPath) where TConfiguration : IConfiguration
		{
			ConfigurationBulder configurationBulder = new ConfigurationBulder(ConfigurationPath);
			var cfg = configurationBulder.GetConfigution<TConfiguration>();
			serviceConfigution = cfg;
			container.RegistSingletonInstance<IConfiguration>(cfg);
			return Start();

		}

		public IHost SetConfiguration<TConfiguration>() where TConfiguration : IConfiguration
		{
			string cfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Configuration.json");
			return SetConfiguration<TConfiguration>(cfgPath);

		}
		public IHost SetConfiguration(string ConfigurationPath)
		{
			return SetConfiguration<ServiceConfigution>(ConfigurationPath);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="SetConfigution"></param>
		/// <returns></returns>
		public IHost SetConfiguration(Action<IConfiguration>SetConfigution)
		{
			serviceConfigution =container.GetService<IConfiguration>();
			if (SetConfigution!=null)
			{
				SetConfigution.Invoke(serviceConfigution);
			}
			
			return Start();
		}
		/// <summary>
		/// 替换容器之后需要重新注册
		/// </summary>
		/// <returns></returns>
		public IHost ReplaceContainer(IContainer container)
		{
			this.container = Container.SetContainer(container);
			Initalization();
			return this;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="action"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException"></exception>
		public IHost Regster(Action<IContainer> action)
		{
			if (action==null)
			{
				throw new ArgumentNullException(nameof(action));
			}
			action.Invoke(container);
			return this;
		}
		/// <summary>
		/// 注册任务然后启动任务
		/// </summary>
		/// <param name="action"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException"></exception>
		/// 
		public IHost RegisterSchedule(Action<IScheduleManager> action)
		{
			if (action==null)
			{
				throw new ArgumentNullException(nameof(action));
			}
			var scheduleManager = container.GetService<IScheduleManager>();
			action.Invoke(scheduleManager);
			return this;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="TMessageHandle"></typeparam>
		/// <returns></returns>
		public IHost ConfigutionMessageHandle<TMessageHandle>() where TMessageHandle : IMessageHandle
		{   
			container.RegisterSingleton<IMessageHandle, TMessageHandle>().Autowird();
			return this;
		}

		
		/// <summary>
		/// 
		/// </summary>
		private Host()
		{
			container = Container.GetContainer();
			Initalization();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="container"></param>
		/// <returns></returns>
		private void Initalization()
		{
			container.RegisterSingleton<ILogger,Logger>().Autowird();
			container.Register<IContext,HandleContext>().Autowird();

			string cfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration.json");
			ConfigurationBulder configurationBulder = new ConfigurationBulder(cfgPath);
			var cfg = configurationBulder.GetConfigution<ServiceConfigution>();
			serviceConfigution = cfg;
			container.RegistSingletonInstance<IConfiguration>(cfg);
			container.RegistSingletonInstance<IConfiguration,ServiceConfigution>(cfg);


			container.RegisterSingleton<IMessageHandle,MessageHandle>().Autowird();
            container.RegisterSingleton<IClientManager,ClientManager>().Autowird();
			container.RegisterSingleton<IScheduleManager,ScheduleManager>().Autowird();
			container.RegisterSingleton<IPretty,Pretty>().Autowird();
			container.Register<IScheduleHandle,ScheduleHandle>().Autowird();
		    logger = container.GetService<ILogger>();
			container.Register<IConnection,Connection>().Autowird();
			logger.Info("容器初始化成功!");
		}
	}
}
